﻿using Microsoft.WowAddonStudio.FrameXml.Components.Support;
using Microsoft.WowAddonStudio.FrameXml.Services;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Actions
{
    public class MakeFrameMovableAction : Action
    {
        public MakeFrameMovableAction(FrameControl control) : base(control)
        {
        }

        /// <summary>
        /// Gets the frame control.
        /// </summary>
        public FrameControl FrameControl
        {
            get { return (FrameControl) this.Control; }

        }
        /// <summary>
        /// Gets or sets the name of the action.
        /// </summary>
        /// <value></value>
        public override string DisplayName
        {
            get
            {
                bool isMovingEnabled = ScriptInjector.IsMovingEnabled(this.FrameControl);
                
                string displayName = (isMovingEnabled)
                                        ? Resources.DisableMovingDisplayMessage
                                        : Resources.EnableMovingDisplayMessage;
                return displayName;
            }
        }

        /// <summary>
        /// Invokes the action.
        /// </summary>
        public override void Invoke()
        {
            try
            {
                var isMovingEnabled = ScriptInjector.IsMovingEnabled(this.FrameControl);
                var property = ScriptInjector.InjectScript(this.FrameControl, ScriptType.Movable);

                // Notify component change service
                RaiseComponentChanged(property, isMovingEnabled, !isMovingEnabled);
            }
            catch (ScriptInjector.ScriptInjectionException ex)
            {
                var service = GetService<IFrameXmlDesignerService>();
                if (service != null)
                    service.NotifyError(ex.Message);
            }
        }
    }
}
